using System;
using System.Collections;



namespace Thinxoft.HttpLib
{
	/// <summary>
	/// This class is the parent class for all XFile pages.  The properties available to XmlPage are available
	/// to your XFile page at all times 
	/// </summary>
    [Serializable]
    public class XmlPage : MarshalByRefObject
	{
		IHttpResponse response;
		IHttpRequest request;
		IHttpServer  serverInfo;

		/// <summary>
		/// creates anew XmlPage
		/// </summary>
		protected XmlPage()
		{
			
		}
		
		/// <summary>
		/// gets a reference to the HttpResponse object
		/// </summary>
		protected IHttpResponse Response
		{
			get{return response;}
		} 						
		
		/// <summary>
		/// gets a reference to the HttpRequest object
		/// </summary>
		protected IHttpRequest Request
		{
			get
			{
				return this.request;
			}
		}

		/// <summary>
		/// gets a reference to the HttpServer object
		/// </summary>
		protected IHttpServer Server
		{
			get
			{
				return this.serverInfo;
			}
		}

		/// <summary>
		/// gets a reference to t he HttpApplicationContect object
		/// </summary>
		protected IHttpApplicationContext Application
		{
			get
			{
				return this.serverInfo.Application;
			}
		}

		/// <summary>
		/// gets a reference to the HttpCookies collection
		/// </summary>
		protected IHttpCookies Cookies
		{
			get
			{
				return this.response.ResponseCookies;
			}
		}

		/// <summary>
		/// gets a reference to the HttpSession object
		/// </summary>
		protected IHttpSession Session
		{
			get
			{
				string sessionId = request.RequestCookies["session"];
				IHttpSession session = (IHttpSession)this.serverInfo.Application[sessionId];
				if(session != null)
					return session;
			else
					return Application.CreateNewSession(sessionId);
			}
		}

		/// <summary>
		/// 
		/// 
		/// </summary>
		/// <param name="request"></param>
		/// <param name="serverInfo"></param>
		/// <param name="resp"></param>		
		public void LoadContext( IHttpRequest request, IHttpServer serverInfo, IHttpResponse resp)
		{
			this.request = request;
			this.serverInfo = serverInfo;
			this.response = resp;
		}

        /// <summary>
        /// 
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="serverInfo"></param>
        /// <param name="resp"></param>		
        public void LoadContext(IHttpRequest request)
        {
            this.request = request;
            response.Clear();
        }	



		/// <summary>
		/// This method is meant for making calls to external xfiles.
		/// Executes the script in the Xfile and returns a response that can be sent back to the client. Calling the internal Execute
		/// of an XFile will ALWAYS produce a StackOverflowException.  This is consistent with the Architecture since
		/// Execute() will run the script where the call to Execute() was  made.  Which wiull in turn run the same script again 
		/// and call Execute() again until the stack overflows 
		/// </summary>
		/// <returns></returns>
		public virtual IHttpResponse Execute()
		{
			return null;
		}
	}
}
